package com.shoutao.website.utils;


import java.util.Collections;
import java.util.List;

/**
* @Description:
* @Param:
* @return:
* @Author: cqin
* @Date: 2019/11/4
*/

public class ObjectUtils {
    /**
     * @param string
     * @return
     */
    public static boolean isNullOrWhiteSpace(String string) {
        return string == null || string.trim().length() == 0;
    }

    /**
     * @param string
     * @return
     */
    public static boolean isNullOrEmpty(String string) {
        return string == null || string.length() == 0;
    }

    /**
     * list为null或者为空
     * @param list
     * @return
     */
    public  static  boolean isNullOrEmpty(List list){
        return list == null || list.isEmpty();
    }

    /**
     * @param integer
     * @return
     */
    public static boolean isNullOrZero(Integer integer) {
        return integer == null || integer == 0;
    }

    /**
     * @param longV
     * @return
     */
    public static boolean isNullOrZero(Long longV) {
        return longV == null || longV == 0;
    }

    /**
     * @param doubleV
     * @return
     */
    public static boolean isNullOrZero(Double doubleV) {
        return doubleV == null || doubleV == 0;
    }

    /***
     * checkNullOrWhiteSpace
     *
     * @param value
     * @param message
     * @param code
     * @return GenResult
     */
    public static GenResult checkNullOrWhiteSpace(String value, String message, String code) {
        if (isNullOrWhiteSpace(value)) {
            return new GenResult(false,code, message);
        }
        return null;
    }

    /***
     * checkNullOrZero
     *
     * @param value
     * @param message
     * @param code
     * @return GenResult
     */
    public static GenResult checkNullOrZero(Integer value, String message, String code) {
        if (isNullOrZero(value) || value <= 0) {
            return new GenResult(false,code, message);
        }
        return null;
    }

    /***
     * checkNullOrZero
     *
     * @param value
     * @param message
     * @param code
     * @return GenResult
     */
    public static GenResult checkNullOrZero(Long value, String message, String code) {
        if (isNullOrZero(value) || value <= 0) {
            return new GenResult(false, code, message);
        }
        return null;
    }

    /***
     * checkNullOrZero
     *
     * @param value
     * @param message
     * @param code
     * @return GenResult
     */
    public static GenResult checkNullOrZero(Double value, String message, String code) {
        if (isNullOrZero(value) || value <= 0) {
            return new GenResult(false,code, message);
        }
        return null;
    }

    /***
     * @param list
     * @return
     */
    public static <T>boolean checkListNullOrEmpty(List<T> list) {
        if (list != null) {
            list.removeAll(Collections.singleton(null));
        }
        return list == null || list.size() == 0;
    }

    /***
     * Check str is Numeric
     * @param str
     * @param message
     * @param code
     * @return if return null is numeric;else str is not numeric
     */
    public static GenResult checkIsNumeric(String str, String message, String code){
        try {
            Double.parseDouble(str);
            return null;
        }catch (NumberFormatException ex){
            return  new GenResult(false,code, message);
        }
    }
}